

class BaseModel {
  constructor(model) {
    this._model = model
  }

  // 查询单条记录
  one(filter = {}) {
    return this._model.findOne(filter)
  }
  
  // 条件判断
  all(filter = {}, sort = { updatedAt: -1 }, skip = 0, limit = 10) {
    return this._model.find(filter).sort(sort).skip(skip).limit(limit)
  }

  // 根据id返回对应记录
  byId(id) {
    return this._model.findById(id)
  }
  // 添加文档
  create(data) {
    return this._model.create(data)
  }
  
  // 修改
  update(filter, data) {
    return this._model.updateMany(filter, { $set: data })
  }
  
  // 删除
  delete(filter) {
    return this._model.deleteMany(filter)
  }
  count(filter) {
    return this._model.countDocuments(filter)
  }
}


module.exports = BaseModel